<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据库管理</title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<script src="js/page.js"></script>
</head>

<body onload="initialize();">
<jsp:useBean id="database" class="DatabaseConnection.DatabaseConnection" />
<%@include file="connect_database.jsp" %>
<%
request.setCharacterEncoding("utf-8");
String database_name = request.getParameter("database");
String table_name = request.getParameter("table");
String sql_columns = "show columns from " + table_name + ";";
ResultSet resultset_columns = database.executeQuery(sql_columns, database_name);
String [] columns = null;
String [] extras = null;
if (resultset_columns != null)
{
	String columns_string = "";
	String extras_string = "";
	while (resultset_columns.next() == true)
	{
		columns_string = columns_string + resultset_columns.getString("Field") + ",";
		extras_string = extras_string + (resultset_columns.getString("Extra").length() > 0 ? resultset_columns.getString("Extra") : "#") + ",";
	}
	columns = columns_string.split(",");	
	extras = extras_string.split(",");
}
String [][] insert_data = new String[columns.length][];
for (int i = 0; i < columns.length; i++)
{
	insert_data[i] = request.getParameter("column_" + columns[i] + "_data").split("#,#");
}
boolean all_success = true;
boolean all_failure = true;
boolean [] success = new boolean[insert_data[0].length];
String sql_insert[] = new String[insert_data[0].length];
for (int i = 0; i < insert_data[0].length; i++)
{
	int first_editable_column_index = -1;
	sql_insert[i] = "insert into " + table_name + "(";
	for (int j = 0; j < columns.length; j++)
	{
		if (!extras[j].equals("auto_increment"))
		{
			if (first_editable_column_index == -1)
				first_editable_column_index = j;
			if (j > first_editable_column_index)
				sql_insert[i]  = sql_insert[i] + ",";
			sql_insert[i] = sql_insert[i] + columns[j];
		}
	}
	sql_insert[i] = sql_insert[i] + ") values (";
	for (int j = 0; j < columns.length; j++)
	{
		if (!extras[j].equals("auto_increment"))
		{
			if (j > first_editable_column_index)
				sql_insert[i]  = sql_insert[i] + ",";
			sql_insert[i] = sql_insert[i] + (insert_data[j][i].equals("#NULL#") ? "null" : "'" + insert_data[j][i] + "'");
		}
	}
	sql_insert[i] = sql_insert[i] + ");";
	success[i] = database.executeUpdate(sql_insert[i]);
	if (success[i] == false)
		all_success = false;
	else
		all_failure = false;
}
database.closeConnection();
if (all_success == true)
{
%>
<script type="text/javascript">
<!--
	window.alert("<%= insert_data[0].length > 1 ? "所有" : "" %>记录添加成功！");
	window.location.href = "records.jsp?database=<%= database_name %>&table=<%= table_name %>";
//-->
</script>
<%
}
else if (all_failure == true)
{
%>
<script type="text/javascript">
<!--
	window.alert("<%= insert_data[0].length > 1 ? "所有" : "" %>记录添加失败！");
	window.location.href = "records.jsp?database=<%= database_name %>&table=<%= table_name %>";
//-->
</script>
<%
}
else
{
%>
<script type="text/javascript">
<!--
	var success_records = "";
	var failure_records = "";
	<%
	for (int i = 0; i < success.length; i++)
	{
		if (success[i] == true)
		{
		%>
		success_records = success_records + (<%= i %> + 1) + ", ";
		<%
		}
		else
		{
		%>
		failure_records = failure_records + (<%= i %> + 1) + ", ";
		<%
		}
	}
	%>
	window.alert("第 " + success_records.substr(0, success_records.length - 2) + " 条记录添成功，第 " + failure_records.substr(0, failure_records.length - 2) + " 条记录添加失败！");
	window.location.href = "records.jsp?database=<%= database_name %>&table=<%= table_name %>";
//-->
</script>
<%
}
%>
</body>
</html>